// 获取当前活动标签页
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  const currentTab = tabs[0];
  if (!currentTab) {
    console.log('获取当前页面失败')
    return;
  }

  // 向当前页面注入脚本以获取localStorage
  chrome.scripting.executeScript({
    target: {tabId: currentTab.id},
    func: () => {
      // 在当前页面环境中执行，获取localStorage
      const storageData = {};
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i);
        storageData[key] = localStorage.getItem(key);
      }
      return storageData;
    }
  }).then((results) => {
    if (results && results[0]) {
      const localStorageData = results[0].result;

      //筛选要过滤数据
      const param = {
        username:localStorageData['username'],
        orgNo:localStorageData['orgNo'],
        Authorization:localStorageData['Authorization'],
        userIp:localStorageData['userIp'],
        token:localStorageData['token']
      }

      // 目标页面URL，这里替换为你的实际页面
      const targetUrl = chrome.runtime.getURL('/client/index.html');

      // 将数据存储到插件本地存储
      chrome.storage.local.set({ storageData: param });

      // 跳转到目标页面并携带参数
      chrome.tabs.create({
        url: `${targetUrl}`
      })
    }

    // 关闭popup
    window.close();
  }).catch((error) => {
    console.error('获取localStorage失败:', error);
    // window.close();
  });
});
